/**
 * @classs Edk.logger 记录日志。
    /**
     * LOG一些SQL操作。读的操作就不LOG。
     * @param rs {ADODB.Recordset} 记录集合
     * @return {ADODB.Recordset}
     */
    function logSQL(rs){
//	    var writeSQL  = /(.*?((insert)|(delete)|(update)|(drop)|(truncate)).*?){2,}/i; 
	    var writeSQL  = /((insert)|(delete)|(update)|(drop)|(truncate))/i; 
    	var hasSource = (typeof rs.Source != 'undefined') && (typeof rs.Source != 'unknown');
        if( hasSource && writeSQL.test(rs.Source)){
//            new $$.logger({sql : rs.Source});
        }
        return rs;
    }
 */
$$.logger = function(a){
    if(a.sql){
        // todo
    	this.add({
    		 type: "sqlLog"
    		,sql : a.sql
    		,createDate: new Date
    	});
    }else if(a.text){
    	this.text = a.text;
    }
}
$$.logger.prototype = {
 	/**
 	 * 
 	 * @param {Object} log
 	 */
 	add: function(log){
 		var XML = $$.XML;
 		var json, tmp;
 		if(log.sql){
 			tmp = log.sql;
 			delete log.sql;
 		}
 		json = XML.json2xml(log);
 		
 		var logItem = new ActiveXObject('Msxml2.DOMDocument.6.0');
 		if(!logItem.loadXML('<item>' + json + '</item>')){
 			throw "生成日志XML文档时错误";
 		}
 		var value = logItem.createElement("value");
 		var cd = logItem.createCDATASection(tmp);
 		
 		value.appendChild(cd);
 		logItem.firstChild.appendChild(value);
		// 插入新的XML节点
 		var xml = new ActiveXObject('Msxml2.DOMDocument.6.0');
 		if(!xml.load(Server.Mappath('/app/public/log.xml'))){
 			throw "保存日志XML文档时错误";
 		}

 		xml.lastChild.appendChild(logItem.firstChild);

		XML.saveXML(xml);
		
		logItem = xml = value = cd = null;
		
		return true;
 	}
 	
 	/**
 	 * 获取客户端的IP地址。
 	 * @return {}
 	 */
 	,getIP : function(isParseInt){
	    var ip = Request.ServerVariables("HTTP_X_FORWARDED_FOR")();
	    // for 0.0.0.0
	    if (!ip || (ip && ip.length < 7)){
	    	 ip = Request.ServerVariables("REMOTE_ADDR")();
	    }
	    
	    // what does this line be used to?
//	    if (strIP.indexOf(",") > 7) strIP = strIP.substr(0, strIP.indexOf(","));
	    if(isParseInt){
        	ip = ip.split('.');
        	ip = Number(ip[0]) * 256 * 256 * 256 +
				 Number(ip[1]) * 256 * 256 +
				 Number(ip[2]) * 256 +
				 Number(ip[3]);
        }
	    return ip;
	}()
};